home="$GAUSSHOME/bin"
ps=(cm_server dssserver gaussdb)
#ps=(gaussdb)
sleep_time=180
run_count=3000
log_post=$(date "+%Y%m%d_%H_%M_%S")
log_path=$(pwd)/binary-$log_post.log



query_state(){
        last_value="1"
        for((jj=1;jj<600;jj++))
        do
                value="1"
                for k in $(cm_ctl query -Cvwdip | grep -A 10 "Datanode State" | awk '{print $9}')
                do
                        if [ "$k" != "Normal" ] ;then
                                value="0"
                               break
                        fi
                done
                last_vaule=$value
                if [ $value == "1" ]; then
                        break
                fi
                sleep 3
        done
        return $last_value

}



for((i=1;i<$run_count;i++))
do
        for p in ${ps[@]}
        do
                cd $home
                echo "-+-+-+-+$(date) $i start mv $p -+-+-+-+" >> $log_path
                mv $p $p"_bak"
                ps ux |grep $p | grep -v grep | awk '{print $2}'|xargs kill -9;sleep 1
                cm_ctl query -Cvipw >> $log_path
                echo "-+-+-+-+$(date) $i rollback mv $p -+-+-+-+" >> $log_path
                mv $p"_bak" $p
                sleep 10
                cm_ctl query -Cvipw >> $log_path
                query_state
                if [ $? == "1" ]; then
                        echo "*********begin in $(date) switchover operate*******" >> $log_path
                        cm_ctl switchover -a >> $log_path
                else
                        exit
                fi
                sleep $sleep_time
        done
done &

